package be.dch.gui.components;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import be.dch.gui.SwingUtils;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class JAddPoint extends JPanel implements ActionListener {

    private static final long serialVersionUID = -5202977746944070175L;
    /**
     * Action command for the Add button.
     */
    public static final String CMD_ADD = "add";

    private static final String COLUMN_SPECS = "1dlu,pref,3dlu,pref,1dlu";
    private static final String ROW_SPECS = "1dlu,pref,3dlu,pref,1dlu";

    private transient JButton btnAdd;
    private transient JTextField txtCount;
    private transient JLabel lblName;
    private transient int counter = 0;
    private boolean selected;
    private final transient Color basicColor = UIManager.getColor("Panel.background");
    private final transient Color selectColor = (Color) UIManager.get("TabbedPane.selected");

    /**
     * Constructor with the name of the Add Point panel
     * 
     * @param name
     */

    public JAddPoint(final String name, final int type) {
	super();
	this.initComponents(name);

	final FormLayout layout = new FormLayout(JAddPoint.COLUMN_SPECS, JAddPoint.ROW_SPECS);

	final CellConstraints constraints = new CellConstraints();

	final PanelBuilder builder = new PanelBuilder(layout);
	this.btnAdd.setName(Integer.toString(type));
	builder.add(this.btnAdd, constraints.rchw(2, 2, 1, 1));
	builder.add(this.txtCount, constraints.rchw(2, 4, 1, 1));
	builder.add(this.lblName, constraints.rchw(4, 2, 1, 3));

	this.add(builder.getPanel());
	this.setBorder(BorderFactory.createLineBorder(Color.black));
    }

    private void initComponents(final String name) {
	this.btnAdd = new JButton("+");
	this.btnAdd.setActionCommand(JAddPoint.CMD_ADD);
	this.btnAdd.addActionListener(this);
	this.txtCount = new JTextField(Integer.toString(this.counter));
	this.txtCount.setBorder(BorderFactory.createLineBorder(Color.black));
	SwingUtils.setFixedDimensions(this.txtCount, 41, 26);
	this.txtCount.setHorizontalAlignment(SwingConstants.CENTER);
	this.txtCount.setBackground(this.getBackground());
	this.lblName = new JLabel("<html><b>" + name + "</b></html>");
	SwingUtils.setFixedDimensions(this.lblName, this.getWidth(), 26);
	this.lblName.setHorizontalAlignment(SwingConstants.CENTER);
    }

    /**
     * Method to add a Listener for the commands
     * 
     * @param listener
     */
    public void addActionListener(final ActionListener listener) {
	this.btnAdd.addActionListener(listener);
    }

    /**
     * Add item to the counter.
     */
    public void addItem() {
	this.counter++;
	this.txtCount.setText(Integer.toString(this.counter));
	// lblCount.repaint();
    }

    /**
     * 
     */
    public void initItem(final int counter) {
	this.counter = counter;
	this.txtCount.setText(Integer.toString(counter));
    }

    @Override
    public Dimension getPreferredSize() {
	return new Dimension(100, 70);
    }

    @Override
    public Dimension getMaximumSize() {
	return this.getPreferredSize();
    }

    @Override
    public Dimension getMinimumSize() {
	return this.getPreferredSize();
    }

    public void actionPerformed(final ActionEvent event) {
	this.addItem();
	this.setSelected(!this.isSelected());

    }

    /**
     * Return if the pane is selected
     * 
     * @return
     */
    public boolean isSelected() {
	return this.selected;
    }

    /**
     * Set if this pane is selected.
     * 
     * @param selected
     */
    public void setSelected(final boolean selected) {
	this.selected = selected;
	if (selected) {
	    this.setColor(this.selectColor);
	} else {
	    this.setColor(this.basicColor);
	}
	this.repaint();
    }

    private void setColor(final Color color) {
	this.setBackground(color);
	for (int i = 0; i < this.getComponentCount(); i++) {
	    final Component comp = this.getComponent(i);
	    comp.setBackground(color);
	}
	this.txtCount.setBackground(color);
    }

    @Override
    public String toString() {
	return this.lblName.getText() + "- selected:" + this.selected;
    }

}
